use "${datadir}\HTB_main_cleaned_counterfactual", replace


*------------------------------------------------------------------------------*
* Merge with new hp appreciation *
*------------------------------------------------------------------------------*


// Appreciation all
rename laua areacode
rename datem datem_original
gen datem = mofd(date) + 48
merge m:1 datem areacode using /// regionname
	""${datadir}\local_price_appreciation_2020.dta", ///
	keep(1 3)  nogen keepusing(appreciation4)

rename datem datem4y
rename appreciation4 fw_appreciation4
rename datem_original datem 


/* 1) House Price (hp) */
gen dhp4 = (fw_appreciation4+1)^(1/4) -1
replace dhp4 = dhp4 * 100


gen hpv4_noHTB_round 	= property_val * (1 + fw_appreciation4) 
gen hpv4_HTB_round 		= cond(htb_london, 0.6 *  property_val * (1 + fw_appreciation4), ///
										0.8 *  property_val * (1 + fw_appreciation4))
gen hpv4_diff_round 	= hpv4_HTB_round - hpv4_noHTB_round 


*------------------------------------------------------------------------------*
* Compute breakeven house-price appreciation and money left on the table *
*------------------------------------------------------------------------------*

gen SVR 			= 3.99/100
		
forval i = 1(1)3 {

		gen refi_external 	= .
		replace refi_external = SVR - 2.38/100  if band_id == 1
		replace refi_external = SVR - 2.26/100  if band_id == 2
		replace refi_external = SVR - 2.23/100  if band_id == 3
		replace refi_external = SVR - 1.87/100  if band_id == 4
		replace refi_external = SVR - 1.87/100  if band_id == 5
		*replace refi_external = SVR - 1.27/100  if band_id == 6
		*replace refi_external = SVR - 1.24/100  if band_id == 7
		replace refi_external = SVR - 1.48/100  if band_id == 6
		replace refi_external = SVR - 1.40/100  if band_id == 7


	if `i' == 1 {
		gen refi_htb 		= refi_external 	
	}	
	if `i' == 2 {
		gen refi_htb 		= . 	
		replace refi_htb = SVR - 2.08/100  		if band_id == 1
		replace refi_htb = SVR - 1.88/100  		if band_id == 2
		replace refi_htb = SVR - 1.87/100  		if band_id == 3
		replace refi_htb = SVR - 1.64/100  		if band_id == 4
		replace refi_htb = SVR - 1.62/100  		if band_id == 5
		replace refi_htb = SVR - 1.48/100  		if band_id == 6
		replace refi_htb = SVR - 1.40/100  		if band_id == 7
	}
	
	if `i' == 3 {
		gen refi_htb 		= SVR 	
	}
	
	
	
	/* 2) Monthly payment (mp)  */

	gen 		payment_refi				= lb_noHTB_round * ( refi_external/12 * (1+refi_external/12)^(mortgage_term * 12) ) ///
												/ ((1+refi_external/12)^(mortgage_term * 12) - 1) 
	gen 		payment_refi_counter		= lb_HTB_round * ( refi_htb/12 * (1+refi_htb/12)^(mortgage_term * 12) ) ///
												/ ((1+refi_htb/12)^(mortgage_term * 12) - 1) 

	gen delta_mortgage_refi	= 3.99 / (100 * 12 )  
	gen delta_credcard_refi = 20 / (100 * 12 ) 			// 

	gen mp_refi_noHTB_round 	= payment_refi
	gen mp_refi_HTB_round 		= payment_refi_counter

	foreach type in mortgage_refi credcard_refi { //  credcard_refi just to have different name from above 
		foreach group in noHTB HTB  { // diff
			gen fvmp_`type'_`group'_round = mp_`group'_round * ( (1+delta_`type')^(dealtype*12) - 1 ) / delta_`type' 
		}
	}


	/* 3) Left balance (lb) */

	gen lb_refi_noHTB_round = lb_noHTB_round * (1+refi_external/12)^(dealtype*12) ///
							-  payment_refi * (((1+refi_external/12)^(dealtype*12) - 1)/(refi_external/12) ) 

	gen lb_refi_HTB_round 	= lb_HTB_round * (1+refi_htb/12)^(dealtype*12) ///
							-  payment_refi_counter * (((1+refi_htb/12)^(dealtype*12) - 1)/(refi_htb/12) ) 


	/* 4) Capitalized refi costs */
							
	gen c_refi_noHTB_round 		= 0 

	gen c_refi_HTB_round		= 0.150 * (1 + delta_mortgage_refi)^(dealtype*12)
					
							
	/* 5) Breakeven house price with refi */

	gen bkhp_withrefi_mortgage = ( ( (fvmp_mortgage_noHTB_round * (1 + delta_mortgage_refi)^(dealtype*12) + lb_refi_noHTB_round ///
										+ c_refi_noHTB_round + fvmp_mortgage_refi_noHTB_round ) ///
										- (fvmp_mortgage_HTB_round * (1 + delta_mortgage_refi)^(dealtype*12) + lb_refi_HTB_round ///
										+ c_refi_HTB_round + fvmp_mortgage_refi_HTB_round ) ) / ///
										( 0.2 * property_val) )^(1/(dealtype*2)) - 1

	replace bkhp_withrefi_mortgage = ( ( (fvmp_mortgage_noHTB_round * (1 + delta_mortgage_refi)^(dealtype*12) + lb_refi_noHTB_round ///
										+ c_refi_noHTB_round + fvmp_mortgage_refi_noHTB_round ) ///
										- (fvmp_mortgage_HTB_round * (1 + delta_mortgage_refi)^(dealtype*12) + lb_refi_HTB_round ///
										+ c_refi_HTB_round + fvmp_mortgage_refi_HTB_round ) ) / ///
										( 0.4 * property_val) )^(1/(dealtype*2)) - 1 if htb_london
									
									
	gen bkhp_full`i'_mortgage = bkhp_withrefi_mortgage * 100
	
	
	* this is money left on the table for case with internal refi
	
	if `i' == 2 {
	
		/* 4) Full = 1) + 2) + 3) */

			
		gen fullnew_mortgage_noHTB_round 	= hpv4_noHTB_round - fvmp_mortgage_noHTB_round*(1 + delta_mortgage_refi)^(dealtype*12) ///
												- fvmp_mortgage_refi_noHTB_round - lb_refi_noHTB_round - c_refi_noHTB_round

		gen fullnew_mortgage_HTB_round 		= hpv4_HTB_round - fvmp_mortgage_HTB_round*(1 + delta_mortgage_refi)^(dealtype*12) ///
												- fvmp_mortgage_refi_HTB_round	- lb_refi_HTB_round - c_refi_HTB_round

		gen fullnew_mortgage_diff_round 	=  fullnew_mortgage_HTB_round - fullnew_mortgage_noHTB_round

											
		gen fullnew_credcard_noHTB_round 	= hpv4_noHTB_round - fvmp_credcard_noHTB_round*(1 + delta_credcard_refi)^(dealtype*12) ///
												- fvmp_credcard_refi_noHTB_round - lb_refi_noHTB_round - c_refi_noHTB_round

		gen fullnew_credcard_HTB_round 		= hpv4_HTB_round - fvmp_credcard_HTB_round*(1 + delta_credcard_refi)^(dealtype*12) ///
												- fvmp_credcard_refi_HTB_round	- lb_refi_HTB_round - c_refi_HTB_round

		gen fullnew_credcard_diff_round 	= fullnew_credcard_HTB_round - fullnew_credcard_noHTB_round
	
	}
	
	drop *refi*

}


*------------------------------------------------------------------------------*
* Charts *
*------------------------------------------------------------------------------*


keep if inrange(bkhp_full2_mortgage, -5, 20)

keep if dealtype==2 & ratetype==1

keep if LTV>60

gen LTV_integer = 5 * ceil(LTV/5)

gen post = year>2015

collapse (count) tot = loan_val (median) bkhp_mortgage bkhp_full*_mortgage, by(LTV_integer /*post*/ )

egen den = sum(tot)
gen share = tot/den


twoway ///
	(connected bkhp_full1_mortgage 	LTV_integer, msize(med) sort) ///
	(connected bkhp_full2_mortgage 	LTV_integer, msize(med) sort msymbol(D)) ///
	(connected bkhp_full3_mortgage 	LTV_integer, msize(med) sort msymbol(X)) ///
	(scatter bkhp_full1_mortgage 	LTV_integer [aweight = share], color(navy)) ///
	(scatter bkhp_full2_mortgage 	LTV_integer [aweight = share], color(maroon) msymbol(D)) ///
	(scatter bkhp_full3_mortgage 	LTV_integer [aweight = share], color(green) msymbol(X)) , ///
	xline(80, lwidth(vthin) lcolor(black) lpattern(dash)) ///	
	ytitle("Break-even house price appreciation with refinancing (%)") ///
	xtitle("Loan-to-value") ///
	legend(ring(0) position(11) r(3) order( 1 "External" 2 "Internal" 3 "Reversion") ) ///
	graphregion(color(white)) bgcolor(white) name(bkhp2, replace)
graph export "${figuredir}\distribution_bkhp_withrefi.pdf", as(pdf) replace